#!/user/bin/env python
# -*- coding:utf-8 -*-
#/home/zc/workspace/tinyfeet/trunk/src/model/Fav.py

'''
添加删除我想去

Created on 2011-9-18

@author: zc


'''

import web
from dbhandle.mysqldb import mysqldb 
from datetime import datetime
from checkkey import checkkey
from controllers.utils import logger
from config.sqlmap import SQL_DEL_IWANT

logger = logger.logger()

class addFav( object ):
    '''  增加收藏    '''
    def POST( self ):
        """ 接受客户端post的消息 """
        data = web.input()
        addres = None
        if checkkey( web.config._session, web.ctx.env ):
            addres = self.addfav( data )
        else:
            addres = '{"msg":"203", "errmsg":"session验证未通过."}' 
        return addres


    def addfav( self, favBody ):
        """
        {
            "uid":1223,
            "tfid":1112,
            }
         """
        cdate = datetime.now().strftime( "%Y%m%d%H%M%S" )
        dbo = mysqldb()
        res = None
        uid = favBody.uid
        tfid = favBody.tfid
        row = dbo.insert( 'tf_favorite', uid = uid, tfid = tfid, date = cdate )
        if row == -1:
            res = '{"msg":"201", "errmsg":"Add Fav Faild."}'
        else:
            res = '{"msg":"200"}'
        return res


class delFav( object ):
    '''  删除收藏    '''
    def POST( self ):
        """ 接受客户端post的消息 """
        data = web.input()
        res = None
        #print data
        if checkkey( web.config._session, web.ctx.env ):
            res = self.delfav( data )
        else:
            res = '{"msg":"203", "errmsg":"session验证未通过."}' 
        return res
    

    def delfav( self, cmtsBody ):
        """  
        {
            "uid":1223,
            "tfid":2213,
            }
         """
        #json_data = simplejson.loads( cmtsBody )
        uid = cmtsBody.uid
        tfid = cmtsBody.tfid
        dbo = mysqldb()
        sql = SQL_DEL_IWANT % ( str( uid ), str( tfid ) ) 
        row = dbo.execsql( sql )
        if row == -1:
            res = '{"msg":"201", "errmsg":"delFav Faild."}'
        else:
            res = '{"msg":"200"}'
        return res
